﻿<!DOCTYPE html><html data-bs-theme="light"><head>
    <meta charset="utf-8">
      <title>C# Coding Standards | docfx seed website </title>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta name="title" content="C# Coding Standards | docfx seed website ">
      
      
      <link rel="icon" href="../favicon.ico">
      <style class="anchorjs"></style><link rel="stylesheet" href="../public/docfx.min.css">
      <link rel="stylesheet" href="../public/main.css">
      <meta name="docfx:navrel" content="../toc.html">
      <meta name="docfx:tocrel" content="toc.html">
      
      <meta name="docfx:rel" content="../">
      
      
      <meta name="docfx:docurl" content="https://github.com/dotnet/docfx/blob/main/samples/seed/articles/csharp_coding_standards.md/#L1">
      <meta name="loc:inThisArticle" content="In this article">
      <meta name="loc:searchResultsCount" content="{count} results for &quot;{query}&quot;">
      <meta name="loc:searchNoResults" content="No results for &quot;{query}&quot;">
      <meta name="loc:tocFilter" content="Filter by title">
      <meta name="loc:nextArticle" content="Next">
      <meta name="loc:prevArticle" content="Previous">
      <meta name="loc:themeLight" content="Light">
      <meta name="loc:themeDark" content="Dark">
      <meta name="loc:themeAuto" content="Auto">
      <meta name="loc:changeTheme" content="Change theme">
      <meta name="loc:copy" content="Copy">
      <meta name="loc:downloadPdf" content="Download PDF">
  <script type="module" src="./../public/docfx.min.js"></script><script>
    const theme = localStorage.getItem('theme') || 'auto'
    document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
  </script></head>

  

  


  <body class="tex2jax_ignore" data-layout="" data-yaml-mime="">
    <header class="bg-body border-bottom">
      <nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
        <div class="container-xxl flex-nowrap">
          <a class="navbar-brand" href="../index.html">
            <img id="logo" class="svg" src="../logo.svg" alt="Seed">
            Seed
          </a>
          <button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
            <i class="bi bi-three-dots"></i>
          </button>
          <div class="collapse navbar-collapse" id="navpanel">
            <div id="navbar">
              <form class="search" role="search" id="search">
                <i class="bi bi-search"></i>
                <input class="form-control" id="search-query" type="search" placeholder="Search" autocomplete="off" aria-label="Search">
              </form>
            
    <ul class="navbar-nav"><li class="nav-item"><a class="nav-link " aria-current="false" href="http://localhost:8089/index.html">Home</a></li><li class="nav-item"><a class="nav-link active" aria-current="page" href="http://localhost:8089/articles/docfx_getting_started.html">Articles</a></li>
            <li class="nav-item dropdown">
              <a href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" class="nav-link dropdown-toggle ">
                API Documentation
              </a>
              <ul class="dropdown-menu"><li class="nav-item"><a class="nav-link " aria-current="false" href="http://localhost:8089/api/BuildFromAssembly.html">.NET API</a></li><li class="nav-item"><a class="nav-link " aria-current="false" href="http://localhost:8089/md/BuildFromAssembly.html">.NET API (markdown)</a></li><li class="nav-item"><a class="nav-link " aria-current="false" href="http://localhost:8089/apipage/BuildFromAssembly.html">.NET API (apipage)</a></li><li class="nav-item"><a class="nav-link " aria-current="false" href="http://localhost:8089/restapi/petstore.html">REST API</a></li></ul>
            </li></ul> 
      <form class="icons">
        <a class="btn border-0" href="https://github.com/dotnet/docfx" title="GitHub"><i class="bi bi-github"></i></a><a class="btn border-0" href="https://twitter.com/" title="Twitter"><i class="bi bi-twitter"></i></a>
        
    <div class="dropdown">
      <a class="btn border-0 dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false" title="Change theme">
        <i class="bi bi-circle-half"></i>
      </a>
      <ul class="dropdown-menu dropdown-menu-end">
        <li><a class="dropdown-item" href="#"><i class="bi bi-sun"></i> Light</a></li>
        <li><a class="dropdown-item" href="#"><i class="bi bi-moon"></i> Dark</a></li>
        <li><a class="dropdown-item" href="#"><i class="bi bi-circle-half"></i> Auto</a></li>
      </ul>
    </div>
      </form></div>
          </div>
        </div>
      </nav>
    </header>

    <main class="container-xxl">
      <div class="toc-offcanvas">
        <div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
          <div class="offcanvas-header">
            <h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
            <button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
          </div>
          <div class="offcanvas-body">
            <nav class="toc" id="toc">
      
      <form class="filter">
        <i class="bi bi-filter"></i>
        <input class="form-control" type="search" autocomplete="off" value="" placeholder="Filter by title" aria-label="Filter by title">
      </form> 
      <div class="flex-fill overflow-y-auto"><ul>
        <li class="  ">
          
          <a class=" nav-link " href="http://localhost:8089/articles/docfx_getting_started.html">Getting Started with docfx</a>
          
        </li>
        <li class=" expander expanded active ">
          <span class="expand-stub"></span>
          <a href="#" class="  ">Engineering Docs</a>
          <ul>
        <li class="  ">
          
          <span class="text-body-tertiary name-only">Section 1</span>
          
        </li>
        <li class="  ">
          
          <a class=" nav-link " href="http://localhost:8089/articles/engineering_guidelines.html">Engineering Guidelines</a>
          
        </li>
        <li class=" active ">
          
          <a class="  " href="http://localhost:8089/articles/csharp_coding_standards.html">C# Coding Standards</a>
          
        </li></ul>
        </li>
        <li class="  ">
          
          <a class=" nav-link " href="http://localhost:8089/articles/markdown.html">Markdown</a>
          
        </li>
        <li class="  ">
          
          <a class=" nav-link " href="https://docs.microsoft.com/en-us/">Microsoft Docs</a>
          
        </li></ul></div>
      <div class="py-2 mb-md-4"><a class="pdf-link" href="http://localhost:8089/articles/seed.pdf">Download PDF</a></div></nav>
          </div>
        </div>
      </div>

      <div class="content">
        <div class="actionbar">
          <button class="btn btn-lg border-0 d-md-none" style="margin-top: -.65em; margin-left: -.8em" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
            <i class="bi bi-list"></i>
          </button>

          <nav id="breadcrumb">
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="http://localhost:8089/articles/docfx_getting_started.html">Articles</a></li><li class="breadcrumb-item"><a href="">Engineering Docs</a></li>
        </ol></nav>
        </div>

        <article data-uid="">
<h1 id="c-coding-standards">C# Coding Standards</h1>

<h2 id="introduction">Introduction<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#introduction" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h2>
<p>The coding standard will be used in conjunction with customized version of <em>StyleCop</em> and <em>FxCop</em> [<strong>TODO</strong>] during both development and build process. This will help ensure that the standard is followed by all developers on the team in a consistent manner.</p>
<blockquote>
<p>"Any fool can write code that a computer can understand. Good programmers write code that humans understand".</p>
<p>Martin Fowler. <em>Refactoring: Improving the design of existing code.</em></p>
</blockquote>
<h3 id="purpose">Purpose<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#purpose" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<p>The aim of this section is to define a set of C# coding standards to be used by CAPS build team to guarantee maximum legibility, reliability, re-usability and homogeneity of our code. Each section is marked <em>Mandatory</em> or <em>Recommended</em>. <em>Mandatory</em> sections, will be enforced during code reviews as well as tools like <em>StyleCop</em> and <em>FxCop</em>, and code will not be considered complete until it is compliant.</p>
<h3 id="scope">Scope<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#scope" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<p>This section contains general C# coding standards which can be applied to any type of application developed in C#, based on <em><a href="http://msdn.microsoft.com/en-us/library/ms229042.aspx" target="_blank" rel="noopener noreferrer nofollow" class="external">Framework Design Guidelines</a></em>.</p>
<p>It does not pretend to be a tutorial on C#. It only includes a set of limitations and recommendations focused on clarifying the development.</p>
<h2 id="tools">Tools<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#tools" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h2>
<ul>
<li><a href="http://www.jetbrains.com/resharper/" target="_blank" rel="noopener noreferrer nofollow" class="external">Resharper</a> is a great 3rd party code cleanup and style tool.</li>
<li><a href="http://stylecop.codeplex.com/" target="_blank" rel="noopener noreferrer nofollow" class="external">StyleCop</a> analyzes C# srouce code to enforce a set of style and consistency rules and has been integrated into many 3rd party development tools such as Resharper.</li>
<li><a href="http://codebox/SDLFxCop" target="_blank" rel="noopener noreferrer nofollow" class="external">FxCop</a> is an application that analyzes managed code assemblies (code that targets the .NET Framework common language runtime) and reports information about the assemblies, such as possible design, localization, performance, and security improvements.</li>
<li><a href="http://toolbox/22561" target="_blank" rel="noopener noreferrer nofollow" class="external">C# Stylizer</a> does many of the style rules automatically</li>
</ul>
<h2 id="highlights-of-coding-standards">Highlights of Coding Standards<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#highlights-of-coding-standards" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h2>
<p>This section is not intended to give a summary of all the coding standards that enabled by our customized StyleCop, but to give a highlight of some rules one will possibly meet in daily coding life. It also provides some recommended however not mandatory(which means not enabled in StyleCop) coding standards.</p>
<h3 id="file-layout-recommended">File Layout (Recommended)<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#file-layout-recommended" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<p>Only one public class is allowed per file.</p>
<p>The file name is derived from the class name.</p>
<pre><code data-highlighted="yes" class="hljs language-bicep">	<span class="hljs-variable">Class</span>   : <span class="hljs-variable">Observer</span>
	<span class="hljs-variable">Filename</span>: <span class="hljs-variable">Observer</span>.<span class="hljs-variable">cs</span>
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<h3 id="class-definition-order-mandatory">Class Definition Order (Mandatory)<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#class-definition-order-mandatory" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<p>The class definition contains class members in the following order, from <em>less</em> restricted scope (public) to <em>more</em> restrictive (private):</p>
<ul>
<li>Nested types, e.g. classes, enum, struct, etc.</li>
<li>Field members, e.g. member variables, const, etc.</li>
<li>Member functions
<ul>
<li>Constructors</li>
<li>Finalizer (Do not use unless absolutely necessary)</li>
<li>Methods (Properties, Events, Operations, Overridables, Static)</li>
<li>Private nested types</li>
</ul>
</li>
</ul>
<h3 id="naming-mandatory">Naming (Mandatory)<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#naming-mandatory" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<ul>
<li><p><strong>DO</strong> use PascalCasing for all public member, type, and namespace names consisting of multiple words.</p>
<pre><code data-highlighted="yes" class="hljs language-bicep">  <span class="hljs-variable">PropertyDescriptor</span>
  <span class="hljs-variable">HtmlTag</span>
  <span class="hljs-variable">IOStream</span>
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
</li>
</ul>
<p><strong>NOTE</strong>: A special case is made for two-letter acronyms in which both letters are capitalized, e.g. <em>IOStream</em></p>
<ul>
<li><p><strong>DO</strong> use camelCasing for parameter names.</p>
<pre><code data-highlighted="yes" class="hljs language-bicep">  <span class="hljs-variable">propertyDescriptor</span>
  <span class="hljs-variable">htmlTag</span>
  <span class="hljs-variable">ioStream</span>
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
</li>
<li><p><strong>DO</strong> start with underscore for private fields</p>
<pre><code data-highlighted="yes" class="hljs language-bicep">  <span class="hljs-variable">private</span> <span class="hljs-variable">readonly</span> <span class="hljs-variable">Guid</span> <span class="hljs-variable">_userId</span> = <span class="hljs-variable">Guid</span>.<span class="hljs-function">NewGuid()</span>;
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
</li>
<li><p><strong>DO</strong> start static readonly fields, constants with capitalized case</p>
<pre><code data-highlighted="yes" class="hljs language-bicep">  <span class="hljs-variable">private</span> <span class="hljs-variable">static</span> <span class="hljs-variable">readonly</span> <span class="hljs-variable">IEntityAccessor</span> <span class="hljs-variable">EntityAccessor</span> = <span class="hljs-literal"><span class="hljs-literal">null</span></span>;
  <span class="hljs-variable">private</span> <span class="hljs-variable">const</span> <span class="hljs-variable">string</span> <span class="hljs-variable">MetadataName</span> = "<span class="hljs-variable">MetadataName</span>";
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
</li>
<li><p><strong>DO NOT</strong> capitalize each word in so-called <a href="http://msdn.microsoft.com/en-us/library/ms229043.aspx" target="_blank" rel="noopener noreferrer nofollow" class="external">closed-form compound words</a>.</p>
</li>
<li><p><strong>DO</strong> have <strong>"Async"</strong> explicitly in the Async method name to notice people how to use it properly</p>
</li>
</ul>
<h3 id="formatting-mandatory">Formatting (Mandatory)<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#formatting-mandatory" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<ul>
<li><strong>DO</strong> use spaces over tabs, and always show all spaces/tabs in IDE</li>
</ul>
<blockquote>
<p><strong>Tips</strong></p>
<p>Visual Studio &gt; TOOLS &gt; Options &gt; Text Editor &gt; C# &gt; Tabs &gt; Insert spaces (Tab size: 4)</p>
<p>Visual Studio &gt; Edit &gt; Advanced &gt; View White Space</p>
</blockquote>
<ul>
<li><p><strong>DO</strong> add <em>using</em> inside <em>namespace</em> declaration</p>
<pre><code data-highlighted="yes" class="hljs language-bicep">  <span class="hljs-variable">namespace</span> <span class="hljs-variable">Microsoft</span>.<span class="hljs-variable">Content</span>.<span class="hljs-variable">Build</span>.<span class="hljs-variable">BuildWorker</span>.<span class="hljs-variable">UnitTest</span>
  {
  	<span class="hljs-property">using</span> <span class="hljs-property">System</span>;
  }
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
</li>
<li><p><strong>DO</strong> add a space when:</p>
<ol>
<li><code>for (var i = 0; i &lt; 1; i++)</code></li>
<li><code>if (a == b)</code></li>
</ol>
</li>
</ul>
<h3 id="cross-platform-coding">Cross-platform coding<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#cross-platform-coding" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<p>Our code should supports multiple operating systems. Don't assume we only run (and develop) on Windows. Code should be sensitvie to the differences between OS's. Here are some specifics to consider.</p>
<ul>
<li><strong>DO</strong> use <code>Enviroment.NewLine</code> instead of hard-coding the line break instead of <code>\r\n</code>, as Windows uses <code>\r\n</code> and OSX/Linux uses <code>\n</code>.</li>
</ul>
<blockquote>
<p><strong>Note</strong></p>
<p>Be aware that thes line-endings may cause problems in code when using <code>@""</code> text blocks with line breaks.</p>
</blockquote>
<ul>
<li><strong>DO</strong> Use <code>Path.Combine()</code> or <code>Path.DirectorySeparatorChar</code> to separate directories. If this is not possible (such as in scripting), use a forward slash <code>/</code>. Windows is more forgiving than Linux in this regard.</li>
</ul>
<h3 id="unit-tests-and-functional-tests">Unit tests and functional tests<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#unit-tests-and-functional-tests" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h3>
<h4 id="assembly-naming">Assembly naming<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#assembly-naming" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h4>
<p>The unit tests for the <code>Microsoft.Foo</code> assembly live in the <code>Microsoft.Foo.Tests</code> assembly.</p>
<p>The functional tests for the <code>Microsoft.Foo</code> assmebly live in the <code>Microsoft.Foo.FunctionalTests</code> assmebly.</p>
<p>In general there should be exactly one unit test assebmly for each product runtime assembly. In general there should be one functional test assembly per repo. Exceptions can be made for both.</p>
<h4 id="unit-test-class-naming">Unit test class naming<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#unit-test-class-naming" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h4>
<p>Test class names end with <code>Test</code> and live in the same namespace as the class being tested. For example, the unit tests for the <code>Microsoft.Foo.Boo</code> class would be in a <code>Microsoft.Foo.Boo</code> class in the test assembly.</p>
<h4 id="unit-test-method-naming">Unit test method naming<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#unit-test-method-naming" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h4>
<p>Unit test method names must be descriptive about <em>what is being tested, under what conditions, and what the expectations are</em>. Pascal casing and underscores can be used to improve readability. The following test names are correct:</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes">PublicApiArgumentsShouldHaveNotNullAnnotation
Public_api_arguments_should_have_not_null_annotation
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<p>The following test names are incorrect:</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes">Test1
Constructor
FormatString
GetData
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<h4 id="unit-test-structure">Unit test structure<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#unit-test-structure" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h4>
<p>The contents of every unit test should be split into three distinct stages, optionally separated by these comments:</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes"><span class="hljs-comment">// Arrange</span>
<span class="hljs-comment">// Act</span>
<span class="hljs-comment">// Assert</span>
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<p>The crucial thing here is the <code>Act</code> stage is exactly one statement. That one statement is nothing more than a call to the one method that you are trying to test. keeping that one statement as simple as possible is also very important. For example, this is not ideal:</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes"><span class="hljs-built_in">int</span> result = myObj.CallSomeMethod(GetComplexParam1(), GetComplexParam2(), GetComplexParam3());
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<p>This style is not recomended because way too many things can go wrong in this one statement. All the <code>GetComplexParamN()</code> calls can throw for a variety of reasons unrelated to the test itself. It is thus unclear to someone running into a problem why the failure occured.</p>
<p>The ideal pattern is to move the complex parameter building into the `Arrange section:</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes"><span class="hljs-comment">// Arrange</span>
P1 p1 = GetComplexParam1();
P2 p2 = GetComplexParam2();
P3 p3 = GetComplexParam3();

<span class="hljs-comment">// Act</span>
<span class="hljs-built_in">int</span> result = myObj.CallSomeMethod(p1, p2, p3);

<span class="hljs-comment">// Assert</span>
Assert.AreEqual(<span class="hljs-number">1234</span>, result);
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<p>Now the only reason the line with <code>CallSomeMethod()</code> can fail is if the method itself blew up.</p>
<h4 id="testing-exception-messages">Testing exception messages<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#testing-exception-messages" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h4>
<p>In general testing the specific exception message in a unit test is important. This ensures that the exact desired exception is what is being tested rather than a different exception of the same type. In order to verify the exact exception it is important to verify the message.</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes"><span class="hljs-keyword">var</span> ex = Assert.Throws&lt;InvalidOperationException&gt;(
    () =&gt; fruitBasket.GetBananaById(<span class="hljs-number">1234</span>));
Assert.Equal(
    <span class="hljs-string">"1234"</span>,
    ex.Message);
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<h4 id="use-xunitnets-plethora-of-built-in-assertions">Use x<wbr>Unit.<wbr>net's plethora of built-in assertions<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#use-xunitnets-plethora-of-built-in-assertions" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h4>
<p>xUnit.net includes many kinds of assertions – please use the most appropriate one for your test. This will make the tests a lot more readable and also allow the test runner report the best possible errors (whether it's local or the CI machine). For example, these are bad:</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes">Assert.Equal(<span class="hljs-literal">true</span>, someBool);

Assert.True(<span class="hljs-string">"abc123"</span> == someString);

Assert.True(list1.Length == list2.Length);

<span class="hljs-keyword">for</span> (<span class="hljs-built_in">int</span> i = <span class="hljs-number">0</span>; i &lt; list1.Length; i++) {
    Assert.True(
        String.Equals
            list1[i],
            list2[i],
            StringComparison.OrdinalIgnoreCase));
}
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<p>These are good:</p>
<pre><code class="lang-cs hljs language-csharp" data-highlighted="yes">Assert.True(someBool);

Assert.Equal(<span class="hljs-string">"abc123"</span>, someString);

<span class="hljs-comment">// built-in collection assertions!</span>
Assert.Equal(list1, list2, StringComparer.OrdinalIgnoreCase);
</code><a class="btn border-0 code-action" href="#" title="Copy"><i class="bi bi-clipboard"></i></a></pre>
<h4 id="parallel-tests">Parallel tests<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="#" href="#parallel-tests" style="margin-left: 0.1875em; padding-right: 0.1875em; padding-left: 0.1875em;"></a></h4>
<p>By default all unit test assemblies should run in parallel mode, which is the default. Unit tests shouldn't depend on any shared state, and so should generally be runnable in parallel. If the tests fail in parallel, the first thing to do is to figure out why; do not just disable parallel tests!</p>
<p>For functional tests it is reasonable to disable parallel tests.</p>

</article>

        <div class="contribution d-print-none">
          <a href="https://github.com/dotnet/docfx/blob/main/samples/seed/articles/csharp_coding_standards.md/#L1" class="edit-link">Edit this page</a>
        </div>

        <div class="next-article d-print-none border-top" id="nextArticle"><div class="prev"><span><i class="bi bi-chevron-left"></i> Previous</span> <a rel="prev" href="http://localhost:8089/articles/engineering_guidelines.html">Engineering Guidelines</a></div> <div class="next"><span>Next <i class="bi bi-chevron-right"></i></span> <a rel="next" href="http://localhost:8089/articles/markdown.html">Markdown</a></div></div>

      </div>

      <div class="affix">
        <nav id="affix">
      <h5 class="border-bottom">In this article</h5>
      <ul><li><a class="link-body-emphasis" href="#introduction">Introduction</a></li><li><a class="link-secondary" href="#purpose">Purpose</a></li><li><a class="link-secondary" href="#scope">Scope</a></li><li><a class="link-body-emphasis" href="#tools">Tools</a></li><li><a class="link-body-emphasis" href="#highlights-of-coding-standards">Highlights of Coding Standards</a></li><li><a class="link-secondary" href="#file-layout-recommended">File Layout (Recommended)</a></li><li><a class="link-secondary" href="#class-definition-order-mandatory">Class Definition Order (Mandatory)</a></li><li><a class="link-secondary" href="#naming-mandatory">Naming (Mandatory)</a></li><li><a class="link-secondary" href="#formatting-mandatory">Formatting (Mandatory)</a></li><li><a class="link-secondary" href="#cross-platform-coding">Cross-platform coding</a></li><li><a class="link-secondary" href="#unit-tests-and-functional-tests">Unit tests and functional tests</a></li></ul></nav>
      </div>
    </main>

    <div class="container-xxl search-results" id="search-results"></div>

    <footer class="border-top text-secondary">
      <div class="container-xxl">
        <div class="flex-fill">
          <span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span>
        </div>
      </div>
    </footer>
  

</body></html>